#ifndef ANALYZER_H
#define ANALYZER_H

#include <vector>
#include <string>
#include <map>
#include <stdexcept>

// 市场类型枚举
enum MarketType {
    CN,    // 沪深市场
    HK,    // 港股市场
    US     // 美股市场
};

struct Stock {
    std::string code;
    std::string name;
    MarketType market = CN;    // 市场类型，默认为沪深
    std::string currency = "CNY"; // 交易币种，默认为人民币
    double price;
    double high;               // 当日最高价
    double low;                // 当日最低价
    double change_percent;
    double volume;
    std::string date;
    std::map<std::string, double> indicators; // 存储技术指标

    // 向后兼容的构造函数
    Stock() = default;
    Stock(std::string c, std::string n, double p) 
        : code(std::move(c)), name(std::move(n)), price(p) {}
};

namespace TechnicalAnalysis {
    // 移动平均
    std::vector<double> calculateMA(const std::vector<double>& prices, int period);
    
    // 指数移动平均
    std::vector<double> calculateEMA(const std::vector<double>& prices, int period);
    
    // MACD
    void calculateMACD(const std::vector<double>& prices,
                     std::vector<double>& dif,
                     std::vector<double>& dea,
                     std::vector<double>& macd);
    
    // RSI
    std::vector<double> calculateRSI(const std::vector<double>& prices, int period = 14);
    
    // 布林带
    struct BollingerBands {
        std::vector<double> upper;
        std::vector<double> middle;
        std::vector<double> lower;
    };
    BollingerBands calculateBollingerBands(const std::vector<double>& prices, int period = 20, double multiplier = 2.0);
    
    // KDJ指标
    struct KDJ {
        std::vector<double> k;
        std::vector<double> d;
        std::vector<double> j;
    };
    KDJ calculateKDJ(const std::vector<double>& highs,
                    const std::vector<double>& lows,
                    const std::vector<double>& closes,
                    int period = 9);
                    
    // ATR(平均真实波幅)
    std::vector<double> calculateATR(const std::vector<double>& highs,
                                   const std::vector<double>& lows,
                                   const std::vector<double>& closes,
                                   int period = 14);
    
    // 策略类型
    enum StrategyType {
        TECHNICAL_ONLY,
        FUNDAMENTAL_ONLY,
        COMBINED,
        CUSTOM
    };
    
    // 自定义策略权重
    struct StrategyWeights {
        double technical = 0.5;
        double fundamental = 0.3;
        double momentum = 0.2;
    };
    
    // 计算策略得分
    std::vector<double> calculateStrategyScore(
        const std::vector<Stock>& stocks,
        StrategyType type = COMBINED,
        const StrategyWeights& weights = StrategyWeights());
};

class StockAnalyzer {
public:
    // 从CSV加载股票数据
    std::vector<Stock> loadFromCSV(const std::string& filename);
    
    // 分析股票并生成推荐
    std::vector<Stock> analyze(const std::vector<Stock>& stocks, 
                              TechnicalAnalysis::StrategyType strategy = TechnicalAnalysis::COMBINED);
    
    // 保存分析结果到CSV
    void saveToCSV(const std::vector<Stock>& stocks, const std::string& filename);
    
    // 添加自定义指标
    void addCustomIndicator(const std::string& name, double value);
    
private:
    std::map<std::string, double> customIndicators_;
};

#endif // ANALYZER_H